import axios from 'axios'

const API_BASE = '/api/media/screenshots'

export const screenshotService = {
  /**
   * 对指定流进行截图
   * @param {number} streamId - 流ID
   * @param {string} outputPath - 输出路径（可选）
   * @returns {Promise} 截图结果
   */
  async takeScreenshot(streamId, outputPath = null) {
    const params = outputPath ? { output_path: outputPath } : {}
    const response = await axios.post(`${API_BASE}/${streamId}/take`, null, { params })
    return response.data
  },

  /**
   * 启动定期截图（用于流监控）
   * @param {number} streamId - 流ID
   * @param {number} interval - 截图间隔（秒）
   * @param {string} outputDir - 输出目录（可选）
   * @returns {Promise} 启动结果
   */
  async startPeriodicScreenshot(streamId, interval = 60, outputDir = null) {
    const params = { interval }
    if (outputDir) params.output_dir = outputDir
    
    const response = await axios.post(`${API_BASE}/${streamId}/start-periodic`, null, { params })
    return response.data
  },

  /**
   * 停止定期截图
   * @param {number} streamId - 流ID
   * @returns {Promise} 停止结果
   */
  async stopPeriodicScreenshot(streamId) {
    const response = await axios.post(`${API_BASE}/${streamId}/stop-periodic`)
    return response.data
  },

  /**
   * 获取指定流的截图列表
   * @param {number} streamId - 流ID
   * @param {number} limit - 返回数量限制
   * @returns {Promise} 截图列表
   */
  async getScreenshots(streamId, limit = 10) {
    const params = { limit }
    const response = await axios.get(`${API_BASE}/${streamId}`, { params })
    return response.data
  },

  /**
   * 获取截图文件URL
   * @param {number} streamId - 流ID
   * @param {string} filename - 文件名
   * @returns {string} 可访问的URL
   */
  getScreenshotUrl(streamId, filename) {
    return `${API_BASE}/${streamId}/${filename}`
  },

  /**
   * 清理旧的截图文件
   * @param {number} streamId - 流ID
   * @param {number} maxAgeDays - 最大保留天数
   * @returns {Promise} 清理结果
   */
  async cleanupScreenshots(streamId, maxAgeDays = 7) {
    const params = { max_age_days: maxAgeDays }
    const response = await axios.delete(`${API_BASE}/${streamId}/cleanup`, { params })
    return response.data
  }
}

export default screenshotService